package com.yulongtian.createMode.Simple_Factory_Pattern.after;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;

/**
 * @author YulongTian
 * @date 2022/7/9 - 16:20
 */
public class XMLUtil {
    //该方法用于从XML配置文件中提取图表类型，并返回类型名
    public static String getChartType() {
        try {
            //创建文档对象
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            String path="E:\\workspace\\JavaProjects\\design_pattern\\src\\com\\yulongtian\\createMode\\Simple_Factory_Pattern\\" +
                    "after\\config.xml";
            doc = builder.parse(path);

            //获取包含图表类型的文本节点
            NodeList nl = doc.getElementsByTagName("chartType");
            Node classNode = nl.item(0).getFirstChild();
            String chartType = classNode.getNodeValue().trim();
            return chartType;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
